package com.atom.module.lib.opengles.geometry

class Line2 {

    val origin: Vec2 = Vec2()

    val direction: Vec2 = Vec2()

    constructor()

    constructor(origin: Vec2, direction: Vec2) {
        this.origin.set(origin)
        this.direction.set(direction)
    }

    constructor(line: Line2) {
        origin.set(line.origin)
        direction.set(line.direction)
    }

    fun set(origin: Vec2, direction: Vec2): Line2 {
        this.origin.set(origin)
        this.direction.set(direction)
        return this
    }

    /**
     * Computes a Cartesian point a specified distance along this line.
     * 该线✖️一个系数 的新的点
     * @throws IllegalArgumentException If the specified result argument is null or undefined.
     */
    fun pointAt(distance: Double): Vec2 {
        return Vec2(
            origin.x + direction.x * distance,
            origin.y + direction.y * distance
        )
    }

    /**
     * 将此行设置为指定的段。 这条线的起点在第一个端点，方向从第一个端点延伸到第二个端点。
     */
    fun setToSegment(pointA: Vec2, pointB: Vec2): Line2 {
        this.origin.set(pointA)
        this.direction.set(pointB.x - pointA.x, pointB.y - pointA.y)
        return this
    }


    fun triStripIntersection(
        points: FloatArray,
        stride: Int,
        elements: ShortArray,
        count: Int,
        result: Vec2
    ): Boolean {
        if (points.size < stride) {
            throw IllegalArgumentException(
                "Line triStripIntersections missingBuffer"
            )
        }
        if (stride < 3) {
            throw IllegalArgumentException(
                "Line triStripIntersections invalidStride"
            )
        }
        if (elements.isEmpty()) {
            throw IllegalArgumentException(
                "Line triStripIntersections missingBuffer"
            )
        }
        throw RuntimeException(
            "Line2 triStripIntersection not run"
        )
    }

    override fun equals(other: Any?): Boolean {
        if (this === other) return true
        if (other == null || javaClass != other.javaClass) return false
        val line: Line2 = other as Line2
        return if (origin != line.origin) false else direction == line.direction
    }

    override fun hashCode(): Int {
        var result = origin.hashCode()
        result = 31 * result + direction.hashCode()
        return result
    }

    override fun toString(): String {
        return "Line2 [origin=$origin, direction=$direction]"
    }
}